package com.kaili.basic.controller.web.v1;


import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.BaseProductQueryDto;
import com.kaili.basic.pojo.dto.SupplierAddDto;
import com.kaili.basic.pojo.dto.SupplierQueryDto;
import com.kaili.basic.pojo.dto.SupplierUpdateDto;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.service.SupplierService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.LoginDetailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 供应商管理 前端控制器
 * </p>
 *
 * @author xxx
 * @since 2019-03-07
 */
@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "供应商管理", description = "供应商管理相关API")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    //新增供应商
    @PostMapping(value = "/suppliers/addSupplier")
    @ApiOperation(value = "新增供应商",notes = "新增供应商")
    public RestStatus addCustomer(@RequestBody SupplierAddDto supplierAddDto){
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierAddDto, supplier);
        Long supplierId = supplierService.getMaxSupplierId();
        supplierId = supplierId + 1L;
        if(supplier.getSupplierCode()==null || "".equals(supplier.getSupplierCode())) {
            supplier.setSupplierCode(StringUtils.leftPad(supplierId.toString(), 5, "0"));
        }
        supplier.setSupplierId(supplierId);
        supplier.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        supplierService.addSupplier(supplier);
        return CommonStatus.ADD_OK;
    }

    //修改供应商
    @PutMapping(value = "/suppliers/updateSupplier")
    @ApiOperation(value = "修改供应商",notes = "修改供应商")
    public RestStatus updateSupplier(@RequestBody SupplierUpdateDto supplierUpdateDto){
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierUpdateDto, supplier);
        supplier.setModifyBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        supplierService.updateSupplier(supplier);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/suppliers/{supplierIds}/deleteSuppliers")
    @ApiOperation(value = "删除供应商", notes = "删除供应商")
    public RestStatus deleteSuppliers(@PathVariable Long[] supplierIds) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",LoginDetailUtils.getCurrentEmployeeId().longValue());
        map.put("supplierIds",supplierIds);
        supplierService.deleteSuppliers(map);
        return CommonStatus.DELETE_OK;
    }

    //条件查询获取供应商信息
    @GetMapping(value = "/suppliers/querySupplier")
    @ApiOperation(value = "条件查询供应商信息",notes = "条件查询供应商信息")
    public Page<Supplier> querySupplier(@ModelAttribute SupplierQueryDto supplierQueryDto) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierQueryDto, supplier);
        return supplierService.querySupplier(new Page<Supplier>(supplier.getPage(),supplier.getPageSize()),supplier);
    }

    //输入框模糊查询获取供应商信息带出计量单位集合和产品类别集合
    @GetMapping(value = "/suppliers/{parameterName}/dimQuerySupplier")
    @ApiOperation(value = "输入框模糊查询供应商信息带出计量单位集合和产品类别集合",notes = "输入框模糊查询供应商信息带出计量单位集合和产品类别集合")
    public List<SupplierSortUnit> dimQuerySupplier(@PathVariable String parameterName) {
        return supplierService.dimQuerySupplier(parameterName);
    }

    //id查询获取供应商信息带出计量单位集合和产品类别集合
    @GetMapping(value = "/suppliers/{supplierId}/querySupplierSortUnit")
    @ApiOperation(value = "id查询获取供应商信息带出计量单位集合和产品类别集合",notes = "id查询获取供应商信息带出计量单位集合和产品类别集合")
    public SupplierSortUnit querySupplierSortUnit(@PathVariable Long supplierId) {
        return supplierService.querySupplierSortUnit(supplierId);
    }

    //查询供应商下的产品（业务操作模块使用）
    @GetMapping(value = "/suppliers/querySupplierProduct")
    @ApiOperation(value = "查询供应商下的产品（业务操作模块使用）",notes = "查询供应商下的产品（业务操作模块使用）")
    public Page<SupplierProduct> querySupplierProduct(@ModelAttribute BaseProductQueryDto productQueryDto) {
        return supplierService.querySupplierProduct(new Page<SupplierProduct>(productQueryDto.getPage(), productQueryDto.getPageSize()), productQueryDto);
    }

    //查询所有供应商（下拉框使用）
    @GetMapping(value = "/suppliers/queryAllSupplier")
    @ApiOperation(value = "查询所有供应商（下拉框使用）",notes = "查询所有供应商（下拉框使用）")
    public List<SupplierAllQuery> queryAllSupplier() {
        return supplierService.queryAllSupplier();
    }

    @GetMapping("/suppliers/export")
    @ApiOperation(value = "导出供应商信息")
    public void export(HttpServletResponse response, @ModelAttribute SupplierQueryDto supplierQueryDto) throws RestException {
        List<SupplierExport> list = supplierService.querySupplierExport(supplierQueryDto);
        ExcelUtil.exportExcel(list,"供应商信息","sheet1", SupplierExport.class, UUID.randomUUID().toString()+".xls",response);
    }
}

